/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mezcla_externa;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
 *
 * @author Usuario
 */
public class PruebaCatalogo {
    public static void main(String[] args) throws FileNotFoundException {
       
       
        Catalogo cat = new Catalogo("archivo");
        Scanner lector = new Scanner(System.in);

        int opcion = 0;
        while (opcion!=5)
        {
            System.out.println("");
            System.out.println("====================");
            System.out.println("      MENU          ");
            System.out.println("====================");
            System.out.println("1. Agregar entrada");
            System.out.println("2. Actualizar entrada");
            System.out.println("3. Eliminar entrada");
            System.out.println("4. Mostrar catalogo");
             System.out.println("5. Salir");

            System.out.print("Ingrese una opcion: ");
            opcion = lector.nextInt();
           

            switch (opcion)
            {
                case 1:
                {
                    System.out.println("Ingrese datos para crear la entrada:");
                    System.out.println("Ingrese atributos de la tabla:");
                    System.out.println("Ingrese nombre de la tabla: ");
                    String nombre = lector.next();
                    System.out.println("Ingrese el numero de atributos de la tabla: ");
                    int n = lector.nextInt();
                    /*crear lista de atributos*/
                    List<Atributo> attrs = new ArrayList<Atributo>();
                    for (int i=0;i<n;i++){
                        System.out.println("Ingrese nombre del atributo: ");
                        String n_atributo = lector.next();
                        int op = 0;
                        while (op!=9)
                        {
                            System.out.println("1. Integer");
                            System.out.println("2. Character");
                            System.out.println("3. Byte");
                            System.out.println("4. Short");
                            System.out.println("5. Long");
                            System.out.println("6. Float");
                            System.out.println("7. Double");
                            System.out.println("8. String");
                            System.out.println("9. Salir");

                           
                            System.out.print("Seleccione tipo de atributo: ");
                            op = lector.nextInt();
                            switch (op)
                            {
                                case 1:{attrs.add(new Atributo(n_atributo,Integer.class));op=9;break;}
                                case 2:{attrs.add(new Atributo(n_atributo,Character.class));op=9;break;}
                                case 3:{attrs.add(new Atributo(n_atributo,Byte.class));op=9;break;}
                                case 4:{attrs.add(new Atributo(n_atributo,Short.class));op=9;break;}
                                case 5:{attrs.add(new Atributo(n_atributo,Long.class));op=9;break;}
                                case 6:{attrs.add(new Atributo(n_atributo,Float.class));op=9;break;}
                                case 7:{attrs.add(new Atributo(n_atributo,Double.class));op=9;break;}
                                case 8:{attrs.add(new Atributo(n_atributo,String.class));op=9;break;}
                                case 9:{break;}
                                default:
                                    break;        
                               
                            }
                        }
                    }
                    Tabla t = new Tabla(nombre, attrs);
                    EntradaCatalogo entrada1 = new EntradaCatalogo(t);
                   
                    if (cat.getNumeroDeTablas()>1){
                        //Catalogo cat1 = null;
                        //deserializar catalogo
                        try{
                            FileInputStream fis = new FileInputStream("catalogo.ser");
                            ObjectInputStream ois = new ObjectInputStream(fis);
                            cat = (Catalogo) ois.readObject();
                            ois.close();
                        }
                        catch(FileNotFoundException e){e.printStackTrace();}
                        catch(IOException e){e.printStackTrace();}
                        catch(ClassNotFoundException e){e.printStackTrace();}
                       
                        /*Al deserealizar*/
                        //System.out.println(cat);
                        cat.crearNuevaEntrada(entrada1);
                        /*al agregar la nueva entrada*/
                        System.out.println(cat);
                        /*serializar catalogo*/
                        try{
                            FileOutputStream fs = new FileOutputStream("catalogo.ser");
                            ObjectOutputStream os = new ObjectOutputStream(fs);
                            os.writeObject(cat);
                            os.close();
                        }
                        catch (FileNotFoundException e){e.printStackTrace();}
                        catch (IOException e){e.printStackTrace();}
                       
                    }
                    else{
                        cat.crearNuevaEntrada(entrada1);
                       /*al agregar la nueva entrada*/
                        System.out.println(cat);
                        try{
                            FileOutputStream fs = new FileOutputStream("catalogo.ser");
                            ObjectOutputStream os = new ObjectOutputStream(fs);
                            os.writeObject(cat);
                            os.close();
                        }
                        catch (FileNotFoundException e){e.printStackTrace();}
                        catch (IOException e){e.printStackTrace();}
                    }
                   
                   
                    //System.out.println(t);
                     System.out.println(cat);
                    break;
                           
                       
                }
                case 2:
                {
                    System.out.print("Ingrese nombre de la tabla a actualizar: ");
                    String nombre = lector.next();
                    System.out.print("Ingrese nuevo nombre: ");
                    String nombre1 = lector.next();
                    try{
                            FileInputStream fis = new FileInputStream("catalogo.ser");
                            ObjectInputStream ois = new ObjectInputStream(fis);
                            cat = (Catalogo) ois.readObject();
                            ois.close();
                        }
                        catch(FileNotFoundException e){e.printStackTrace();}
                        catch(IOException e){e.printStackTrace();}
                        catch(ClassNotFoundException e){e.printStackTrace();}
                    //cat.getEntradas().get(nombre);
                    cat.ActualizarEntrada(nombre, nombre1);
                    try{
                            FileOutputStream fs = new FileOutputStream("catalogo.ser");
                            ObjectOutputStream os = new ObjectOutputStream(fs);
                            os.writeObject(cat);
                            os.close();
                        }
                        catch (FileNotFoundException e){e.printStackTrace();}
                        catch (IOException e){e.printStackTrace();}
                    break;
                   
                }
                case 3:
                {
                    System.out.print("Ingrese nombre de la tabla a eliminar: ");
                    String nombre = lector.next();
                    try{
                            FileInputStream fis = new FileInputStream("catalogo.ser");
                            ObjectInputStream ois = new ObjectInputStream(fis);
                            cat = (Catalogo) ois.readObject();
                            ois.close();
                        }
                        catch(FileNotFoundException e){e.printStackTrace();}
                        catch(IOException e){e.printStackTrace();}
                        catch(ClassNotFoundException e){e.printStackTrace();}
                    cat.EliminarEntrada(nombre);
                    try{
                            FileOutputStream fs = new FileOutputStream("catalogo.ser");
                            ObjectOutputStream os = new ObjectOutputStream(fs);
                            os.writeObject(cat);
                            os.close();
                        }
                        catch (FileNotFoundException e){e.printStackTrace();}
                        catch (IOException e){e.printStackTrace();}
                    break;
                }
               
                case 4:
                {
                    System.out.println("CATALOGO");
                    try{
                            FileInputStream fis = new FileInputStream("catalogo.ser");
                            ObjectInputStream ois = new ObjectInputStream(fis);
                            cat = (Catalogo) ois.readObject();
                            ois.close();
                        }
                        catch(FileNotFoundException e){System.out.println("Catalogo sin tablas");}
                        catch(IOException e){System.out.println("Catalogo sin tablas");}
                        catch(ClassNotFoundException e){System.out.println("Catalogo sin tablas");}
                       
                        /*Al deserealizar*/
                        System.out.println(cat);
                    break;
                }
               
                case 5:
                {
                    break;
                }

                default:
                    break;
            }
        }
    }
   
}

